﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace Common
{
    public class FileHelper
    {
        public Result SaveImage(HttpPostedFileBase file, string uid, string filePath)
        {
            try
            {
                var hz = Path.GetExtension(file.FileName.Trim());
                String[] allowedExtensions = { ".jpg", ".png", ".jpeg" }; //允许上传的文件格式
                for (int i = 0; i < allowedExtensions.Length; i++)
                {
                    if (hz == allowedExtensions[i])
                    {
                        return new Result() { Success = false, Msg = "只支持上传jpg/png/jpeg格式的图片文件" };
                    }
                }
                var fileName = uid + hz;
                var savePath =HttpContext.Current.Server.MapPath(filePath);
                file.SaveAs(Path.Combine(savePath, fileName));
                return new Result() { Success = true, Msg = fileName };
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
                return new Result() { Success = false, Msg = "上传失败" };
            }
        }
    }
}
